﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;

namespace BLL
{
    public class PJSampleOutManager
    {
        private PJSampleOutService objPJSampleOutService = new PJSampleOutService();

        public string InsertIsNoSucess(PJSampleOut main)
        {
            string result;
            string serialNum = GetLastSerialNum(main.OrderType,main.OrderStartSerialNum.Substring(6,2));
            if (!string.IsNullOrEmpty(serialNum))
            {
                int temp = int.Parse(serialNum.Substring(4, 4));
                temp++;
                serialNum = "2" + main.SerialNum + temp.ToString().PadLeft(4, '0');
                main.SerialNum = serialNum;
                if(main.OrderType=="免费订单")
                {
                    main.Bill = main.SerialNum;
                }
                else
                {
                    main.Bill = "";
                }
                foreach (var item in main.ChildList)
                {
                    item.SerialNum = main.SerialNum;
                }
            }
            else
            {
                main.SerialNum = "2" + main.SerialNum + "0001";
                if (main.OrderType == "免费订单")
                {
                    main.Bill = main.SerialNum;
                }
                else
                {
                    main.Bill = "";
                }
                foreach (var item in main.ChildList)
                {
                    item.SerialNum = main.SerialNum;
                }
            }
            int a = IsNoSampleOut(main.WorkParentId);
            if (a == 0)
            {
                bool msg = InsertInfo(main);
                if (msg == true)
                {
                    result = main.SerialNum;
                }
                else
                {
                    result = "false";
                }
            }
            else
            {
                result = "false";
            }
            return result;
        }

        public int UpdateSaleOrder(string saleOrder, int workParentId)
        {
            return objPJSampleOutService.UpdateSaleOrder(saleOrder, workParentId);
        }
        public int UpdateBill(string bill, int workParentId)
        {
            return objPJSampleOutService.UpdateBill(bill, workParentId);
        }
        public int UpdateBill2(string bill2, int workParentId)
        {
            return objPJSampleOutService.UpdateBill2(bill2, workParentId);
        }
        public int UpdateStatues(int workParentId)
        {
            return objPJSampleOutService.UpdateStatues(workParentId);
        }
        private bool InsertInfo(PJSampleOut main)
        {
            return objPJSampleOutService.InsertInfo(main);
        }
        public void UpdateInfo(PJSampleOut main)
        {
            foreach (var item in main.ChildList)
            {
                 item.SerialNum = main.SerialNum;
            }
            objPJSampleOutService.UpdateInfo(main);
            int childListNum = objPJSampleOutService.GetChildListNum(main.SerialNum);
            if (childListNum < main.ChildList.Count)
            {
                main.ChildList = main.ChildList.Where(s => s.Row > childListNum).ToList();
                //添加新行项目
                objPJSampleOutService.InsertChildInfo(main);
            }
            else if (childListNum > main.ChildList.Count)
            {
                objPJSampleOutService.DelChildList(main.SerialNum, main.ChildList.Count);
                //删除旧行项目
            }

        }
        private string GetLastSerialNum(string orderType,string right2)
        {
            return objPJSampleOutService.GetLastSerialNum(orderType, right2);
        }
        public PJSampleOut GetSampleOut(int workParentId)
        {
            return objPJSampleOutService.GetSampleOut(workParentId);
        }
        public List<PJSampleExport1> ExportSampleOut1(int workParentId)
        {
            return objPJSampleOutService.ExportSampleOut1(workParentId);
        }
        public List<PJSampleExport2> ExportSampleOut2(int workParentId)
        {
            return objPJSampleOutService.ExportSampleOut2(workParentId);
        }
        public int IsNoSampleOut(int workParentId)
        {
            return objPJSampleOutService.IsNoSampleOut(workParentId);
        }
    }
}
